package node

import (
	"context"
	"fmt"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
	"strings"
)

func Get_pods_by_node(v1 corev1.CoreV1Interface, ns_list []string,
	v1_node []v1_node) {
	pod_array := make([]string, 0)
	for _, k := range ns_list {
		podList, _ := v1.Pods(k).List(context.TODO(), metav1.ListOptions{})
		for _, pod := range podList.Items {
			// nginx:vms72:default
			pod_array = append(pod_array, pod.Name+":"+pod.Spec.NodeName+":"+k)
		}
	}

	vo_arr := make([]*PodsVO, 0)
	//设置node节点
	//vms72
	for _, node := range v1_node {
		vo_arr = append(vo_arr, &PodsVO{
			node.node_name,
			nil,
		})
	}

	//设置pod内容
	for _, vo := range vo_arr {
		name := vo.Node_name
		list := make([]string, 0)

		for _, pod := range pod_array {
			if strings.Contains(pod, name) {
				list = append(list, strings.Split(pod, ":")[0])
			}
		}
		vo.Set_Pods_List(list)
	}

	for _, vo := range vo_arr {
		fmt.Println(vo.Node_name)
		for _, pods := range vo.Pods_list {
			fmt.Print(pods + "\t")
		}
		fmt.Println()
	}
}
